<?php
function plugin_render() {
    $input = '';
    $output = '';
    $error = '';
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $input = trim($_POST['json'] ?? '');
        $data = json_decode($input, true);
        if ($data === null && $input !== 'null' && $input !== '') {
            $error = 'JSON格式错误';
        } else {
            $output = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        }
    }
    ?>
    <form method="post" class="space-y-4">
      <textarea name="json" rows="6" class="w-full border rounded p-2" placeholder="请输入JSON字符串"><?=htmlspecialchars($input)?></textarea>
      <button type="submit" class="bg-blue-500 text-white px-6 py-2 rounded">格式化</button>
      <?php if ($error): ?>
        <div class="text-red-500"><?=htmlspecialchars($error)?></div>
      <?php endif; ?>
    </form>
    <?php if ($output): ?>
      <div class="mt-4">
        <pre class="bg-gray-50 border rounded p-2 text-xs overflow-x-auto"><?=htmlspecialchars($output)?></pre>
      </div>
    <?php endif;
} 